
/**
 * 补差计时器
 * @param {Function} callback 回调函数
 * @param {Number} interval 时间间隔
 * @param {Boolean} firstCall 是否首次立即回调

  example:
  $timer(function (timer, count) {
    console.log('$timer ', ...arguments)
    let date = new Date()
    document.writeln(`${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}.${date.getMilliseconds()}`)

    // 暂停计时器, 在 callback 里 clearTimeout
    // clearTimeout(timer)
  }, 1000, 1)
 */

window.$timer = (callback = () => { }, interval = 1000, firstCall = false) => {
  let count = 0
  let start = +new Date()
  // start = start.getTime() - start.getMilliseconds()
  let timer
  console.log('  ⏰ -> start:' + start + ', interval:' + interval)

  const loop = () => {
    let diff = +new Date() - (start + count * interval)
    let next = interval - diff
    if (next < 0) next = 0
    // console.log('  -> count:' + count + ', diff:' + diff + 'ms, next:' + next)

    count++

    timer = setTimeout(loop, next)

    callback(timer, count)
  }

  // 非首次执行时 需将count++ 因为在实际第一次执行时已经有一个时间间隔的延迟
  if (firstCall) loop()
  else timer = setTimeout(loop, interval, count++)
}


const ua = navigator.userAgent
const ua2 = ua.toLowerCase()
// 全局环境变量
window.ENV = {
  debug: process.env.NODE_ENV !== 'production',
  version: '3.1.0',
  ua: {
    mobile: !!ua.match(/AppleWebKit.*Mobile.*/) || !!ua.match(/AppleWebKit/),

    ios: !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
    android: ua.indexOf('Android') > -1,

    eyee: ua2.indexOf('platform/app') !== -1,

    wx: /micromessenger/.test(ua2),
    qq: /(iPad|iPhone|iPod).*? (IPad)?QQ\/([\d\.]+)/.test(ua2) || /\bV1_AND_SQI?_([\d\.]+)(.*? QQ\/([\d\.]+))?/.test(ua2), // /qq/.test(ua2),
    weibo: /weibo/.test(ua2),
    alipay: /alipayclient/.test(ua2)
  }
}
